package com.jcl.recruit.crawler.logic;

import com.jcl.core.utils.CollectionUtils;
import com.jcl.recruit.crawler.dao.CrawlerDownloadTaskDao;
import com.jcl.recruit.crawler.model.CrawlerDownloadTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName CrawlerDownloadTaskLogic

 * @Author yyliu
 * @Date 2018/10/22 14:44
 * @Version 1.0
 **/
@Component
public class CrawlerDownloadTaskLogic {

    @Autowired
    private CrawlerDownloadTaskDao crawlerDownloadTaskDao;

    public List<Integer> findResumeIdListByUser(String corpCode, Integer userId) {
        List<Integer> ids = new ArrayList<>();
        List<CrawlerDownloadTask> list = crawlerDownloadTaskDao.findResumeListByUser(corpCode, userId);
        if (CollectionUtils.isNotEmpty(list)) {
            for (CrawlerDownloadTask task : list) {
                ids.add(task.getResumeId());
            }
        }
        return ids;
    }

    public Map<Integer, List<Integer>> findResumeIdListByUserIds(String corpCode, List<Integer> userIds) {
        Map<Integer, List<Integer>> userMap = new HashMap<>();
        List<CrawlerDownloadTask> list = crawlerDownloadTaskDao.findResumeListByUserIds(corpCode, userIds);
        if (CollectionUtils.isNotEmpty(list)) {
            for (CrawlerDownloadTask task : list) {
                List<Integer> temp = userMap.get(task.getUserId());
                if (null == temp) {
                    temp = new ArrayList<>();
                    userMap.put(task.getUserId(), temp);
                }
                temp.add(task.getResumeId());
            }
        }
        return userMap;
    }

    public List<CrawlerDownloadTask> findListByDown(String corpCode, String channelType, Integer... statusList) {
        return crawlerDownloadTaskDao.findListByDown(corpCode, channelType, statusList);
    }

    public List<CrawlerDownloadTask> findListByResumeId(String corpCode, Integer resumeId) {
        return findList(corpCode, null, resumeId, null);
    }

    public List<CrawlerDownloadTask> findList(String corpCode, String channelType, Integer resumeId, Integer status) {
        return crawlerDownloadTaskDao.findList(corpCode, channelType, resumeId, status);
    }

    public List<CrawlerDownloadTask> findResumeListByUser(String corpCode, Integer userId) {
        return crawlerDownloadTaskDao.findResumeListByUser(corpCode, userId);
    }

    public List<CrawlerDownloadTask> findResumeListByUserIds(String corpCode, List<Integer> userIds) {
        return crawlerDownloadTaskDao.findResumeListByUserIds(corpCode, userIds);
    }

    public int add(CrawlerDownloadTask crawlerDownloadTask) {
        return crawlerDownloadTaskDao.add(crawlerDownloadTask);
    }

    public int updateByResumeIds(List<Integer> resumeIds, Integer status) {
        return crawlerDownloadTaskDao.update(resumeIds, status);
    }

    public int update(List<Integer> ids, Integer status) {
        return crawlerDownloadTaskDao.update(ids, status);
    }

    public int updateTimer(String date, Integer status, String corpCode, String resumeId) {
        return crawlerDownloadTaskDao.updateTimer(date, status, corpCode, resumeId);
    }
}
